package com.ss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ss.bean.ZhCustomer;
import com.ss.mapper.ZhCustomerMapper;
import com.ss.service.ZhCustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ss.vo.CustomerMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 业主信息表 服务实现类
 * </p>
 *
 * @author lian
 * @since 2022-09-16
 */
@Service
public class ZhCustomerServiceImpl extends ServiceImpl<ZhCustomerMapper, ZhCustomer> implements ZhCustomerService {

    @Resource
    private ZhCustomerMapper zhCustomerMapper;

    @Override
    public Integer insertCustomer(ZhCustomer zhCustomer) {
        Integer result = 0;
        QueryWrapper<ZhCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_code", zhCustomer.getCustomerCode());
        ZhCustomer customer = zhCustomerMapper.selectOne(queryWrapper);
        if (customer == null) {
            result = zhCustomerMapper.insert(zhCustomer);
        }
        return result;
    }

    @Override
    public List<ZhCustomer> selectAllCustomer() {
        List<ZhCustomer> zhCustomers = zhCustomerMapper.selectAll();
        return zhCustomers;
    }

    @Override
    public List<ZhCustomer> selectCustomerByColumnAndValue(String column, String value) {
        QueryWrapper<ZhCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column, value);
        List<ZhCustomer> zhCustomers = zhCustomerMapper.selectList(queryWrapper);
        return zhCustomers;
    }

    @Override
    public List<ZhCustomer> selectByCustomerByCustomerType(String customerType) {
        List<ZhCustomer> zhCustomers;
        if (customerType.equals("")) {
            return selectAllCustomer();
        } else {
            QueryWrapper<ZhCustomer> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("customer_type", customerType);
            zhCustomers = zhCustomerMapper.selectList(queryWrapper);
        }
        return zhCustomers;
    }

    /**
     * 查询优化方法，统一查询入口
     *
     * @param customerMessage 用户传递的参数
     * @return
     */
    @Override
    public List<ZhCustomer> selectCustomer(CustomerMessage customerMessage) {
        QueryWrapper<ZhCustomer> queryWrapper = new QueryWrapper<>();
        // 保证column和value属性都有值(根据两个参数精准查询功能)
        if (!StringUtils.isEmpty(customerMessage.getColumn()) && !StringUtils.isEmpty(customerMessage.getValue())) {
            queryWrapper.eq(customerMessage.getColumn(), customerMessage.getValue());
        }
        // 保证customerType属性有值(根据业主类型查询业主信息功能)
        if (!StringUtils.isEmpty(customerMessage.getCustomerType())) {
            queryWrapper.eq("customer_type", customerMessage.getCustomerType());
        }
        // 如果上述条件都不满足，直接做一个全部数据查询
        List<ZhCustomer> zhCustomers = zhCustomerMapper.selectList(queryWrapper);
        return zhCustomers;
    }

    @Override
    public Integer UpdateCustomerStatusByCustomerCode(String customerCodes, String status) {
        Integer result = 0;
        // 如果customerCodes包含"|"就为True，不包含为False
        if (customerCodes.contains("|")) {
            // 需要拆分具体的业主编号
            // 11|22|33|44
            String[] codes = customerCodes.split("[|]");
            for (int i = 0; i < codes.length; i++) {
                UpdateWrapper<ZhCustomer> updateWrapper1 = new UpdateWrapper<>();
                updateWrapper1.set("customer_status", status).eq("customer_code", codes[i]);
                result = zhCustomerMapper.update(null, updateWrapper1);
            }
            return result;
        } else {
            // 只选择了一个业主信息
            // 这里要注意必需重新创建UpdateWrapper对象，否则，查询条件结果会累加
            UpdateWrapper<ZhCustomer> updateWrapper2 = new UpdateWrapper<>();
            updateWrapper2.set("customer_status", status).eq("customer_code", customerCodes);
            result = zhCustomerMapper.update(null, updateWrapper2);
            return result;
        }
    }

}
